//
//  KZBaseViewController.m
//  kangzhipifuyisheng
//
//  Created by 唐开江 on 15/5/26.
//  Copyright (c) 2015年 KangZhi. All rights reserved.
//

#import "KZBaseVC.h"
#import "KZBaseNoDataView.h"
#import "KZBaseLoadView.h"
#import "KZBaseReloadView.h"
#import "KZNaviVC.h"
#import "KZLogin.h"
#import "AppDelegate.h"
#import "KZMineVC.h"
@interface KZBaseVC ()


@property (nonatomic,strong)KZBaseNoDataView * viewEmpty;
@property (nonatomic,strong)KZBaseLoadView * viewLoad;
@property (nonatomic,strong)KZBaseReloadView * reloadView;

@end

@implementation KZBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = globalBackgroundColor;
//    self.viewEmpty = [[KZBaseNoDataView alloc] initWithFrame:self.view.bounds];
//    self.viewLoad = [[KZBaseLoadView alloc] initWithFrame:self.view.bounds];
//    self.reloadView = [[KZBaseReloadView alloc]initWithFrame:self.view.bounds];
    self.view.userInteractionEnabled = YES;
    

}

- (KZBaseNoDataView *)viewEmpty
{
    if (_viewEmpty ==nil) {
    _viewEmpty = [[KZBaseNoDataView alloc] initWithFrame:self.view.bounds];
    }
    return _viewEmpty;
}
- (KZBaseLoadView *)viewLoad
{
    if (_viewLoad ==nil) {
        _viewLoad = [[KZBaseLoadView alloc] initWithFrame:self.view.bounds];
    }
    return _viewLoad;
}
- (KZBaseReloadView *)reloadView
{
    if (_reloadView ==nil) {
        _reloadView = [[KZBaseReloadView alloc] initWithFrame:self.view.bounds];
    }
    return _reloadView;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


//统一不延伸
- (id)init {
    if (self = [super init]) {
        if ([self respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)])
        {
            self.extendedLayoutIncludesOpaqueBars = NO;
        }
        if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
        {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
        if([self respondsToSelector:@selector(setModalPresentationCapturesStatusBarAppearance:)])
        {
            self.modalPresentationCapturesStatusBarAppearance = YES;
        }
    }
    return self;
}


+ (UIViewController *)presentingVC{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
//    if ([result isKindOfClass:[KZMineVC class]]) {
//        result = [(KZMineVC *)result selectedViewController];
//    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}


//present 控制器
+ (void)presentVC:(UIViewController *)viewController{
    UINavigationController *nav = [[KZNaviVC alloc] initWithRootViewController:viewController];
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:viewController action:@selector(dismissModalViewControllerAnimated:)];
    [[self presentingVC] presentViewController:nav animated:YES completion:nil];
}

#pragma mark Login

//设置登陆
- (void)loginOutToLoginVC{
    [KZLogin doLogout];
    [((AppDelegate *)[UIApplication sharedApplication].delegate) setupLoginVC];
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
    NSLog(@"%@ dealloc", [self class]);
}

#pragma mark - Empty
- (void)showEmpty
{
    self.viewEmpty.frame = self.view.bounds;
    [self.view addSubview:self.viewEmpty];
}

- (void)showEmpty:(CGRect)frame
{
    self.viewEmpty.frame = frame;
    [self.view addSubview:self.viewEmpty];
}

- (void)hideEmpty
{
    [self.viewEmpty removeFromSuperview];
    self.viewEmpty = nil;
}
#pragma mark - load
- (void)showLoading
{
    self.viewLoad.frame = self.view.bounds;
    [self.view addSubview:self.viewLoad];
}
- (void)hideLoading
{
    [self.viewLoad removeFromSuperview];
    self.viewLoad = nil;
}

#pragma  mark - reload

- (void)showReloadWithReloadBlock:(KZBaseReloadViewBlock)reloadBlock;
{
    self.reloadView.frame = self.view.bounds;
    [self.view addSubview:self.reloadView];
    self.reloadView.reloadBlock = reloadBlock ;
}

- (void)hideReload
{
    [self.reloadView removeFromSuperview];
    self.reloadView = nil;
}



@end
